package com.wy.controller;

import com.wy.pojo.User;
import com.wy.repository.UserTestDao;
import com.wy.service.UserService;
import com.wy.pojo.UserTest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.PageRequest;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.awt.print.Pageable;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

/**
 * @Author: wy
 * @Date:2019/10/27 11:56
 */
@RestController
@RequestMapping("/test")
public class UserTestController {
     @Autowired
      private UserService userService;
   @Autowired
    private UserTestDao userTestDao;


      @RequestMapping("/saveAll")
     public void saveAll(){
           //存入到索引库中
         List<UserTest> list=new ArrayList<>();
          //得到所有用户信息
          List<User> users =userService.findAll();
          for(User user:users){
              UserTest userTest=new UserTest();
              userTest.setAge(user.getAge());
              userTest.setDesc(user.getUserName());
              userTest.setSex(user.getSex().toString());
              userTest.setId(user.getId());
              userTest.setName(user.getName());
              list.add(userTest);
          }
             userTestDao.saveAll(list);
     }

      @RequestMapping("/findAll/{page}/{size}")
     public String findAll(@PathVariable Integer page,@PathVariable Integer size){
           //分页查询
          PageRequest pageRequest = new PageRequest(page, size);
          //从索引库中查找
          Iterable<UserTest> userTests = userTestDao.findAll(pageRequest);
             for(UserTest user:userTests){
                 System.out.println(user);
             }
           return "查询成功!";
      }
        @RequestMapping("/delete")
       public void delete(){
            userTestDao.deleteAll();
      }


}
